package com.sywb.chuangyebao.widget;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
/**
 * Created by pxb on 2018/6/15.
 */
public class AutoHeightViewPager extends SuperViewPager{

    public AutoHeightViewPager(Context context) {
        super(context);
    }
    public AutoHeightViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int indent = getCurrentItem();
        int weights = 0;
        if (getAdapter()!=null){
            Fragment fragment = ((FragmentStatePagerAdapter)getAdapter()).getItem(indent);
            if(fragment.getView()!= null){
                View view = fragment.getView();
                view.measure(widthMeasureSpec , MeasureSpec.makeMeasureSpec(0 , MeasureSpec.UNSPECIFIED));
                weights = view.getMeasuredHeight();
            }else{
            }
            weights = MeasureSpec.makeMeasureSpec(weights , MeasureSpec.EXACTLY);
        }
        super.onMeasure(widthMeasureSpec, weights);
    }
}
